package com.ysd.lis.controller.sta;


import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.ysd.common.Result;
import com.ysd.lis.annotation.LogDetail;
import com.ysd.lis.aspect.OperationType;
import com.ysd.lis.controller.pub.BaseController;
import com.ysd.lis.entity.BaseEntity;
import com.ysd.lis.entity.sta.StaGermMorphologyType;
import com.ysd.lis.request.MorphologyTypeParam;
import com.ysd.lis.service.sta.StaGermMorphologyTypeService;
import com.ysd.util.PropertySlash;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 菌落形态类 前端控制器
 * </p>
 *
 * @author wanghao
 * @since 2024-04-03
 */
@RestController
@RequestMapping("/sta/germ/morphologyType")
public class StaGermMorphologyTypeController extends BaseController {

    @Autowired
    private StaGermMorphologyTypeService bacGermMorphologyTypeService;

    @Value("${morphologyPath}")
    private String picturePath;

    private static final int MAX_POST_SIZE = 20 * 1024 * 1024;

    @ApiOperation("获取菌落形态类（分页）")
    @GetMapping("/findPageList")
    public Result findPageList(MorphologyTypeParam morphologyTypeParam) {
        return bacGermMorphologyTypeService.findPageList(morphologyTypeParam);
    }

    @ApiOperation("通过类型获取菌落形态")
    @GetMapping("/findListByType")
    public Result findListByType(StaGermMorphologyType bacGermMorphologyType) {
        return bacGermMorphologyTypeService.findListByType(bacGermMorphologyType);
    }

    @ApiOperation("修改或新增单个菌落形态类")
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody StaGermMorphologyType morphologyType) {
        boolean b = bacGermMorphologyTypeService.saveOrUpdate(morphologyType);
        if (b) {
            return Result.succ(1, "保存成功", "");
        } else {
            return Result.succ(400, "保存失败", "");
        }
    }
    @ApiOperation("修改或新增菌落形态类")
    @PostMapping("/saveOrUpdateBatch")
    public Result saveOrUpdateBatch(@RequestBody List<StaGermMorphologyType> morphologyType) {
        boolean b = bacGermMorphologyTypeService.saveOrUpdateBatch(morphologyType);
        if (b) {
            return Result.succ(1, "保存成功", "");
        } else {
            return Result.succ(400, "保存失败", "");
        }
    }

    @ApiOperation("批量删除菌落形态类")
    @PostMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<StaGermMorphologyType> morphologyType) {
        List<String> ids = morphologyType.stream().filter(item -> StringUtils.isNotBlank(item.getId())).map(BaseEntity::getId).collect(Collectors.toList());
        boolean b = bacGermMorphologyTypeService.removeByIds(ids);
        if (b) {
            return Result.succ(1, "保存成功", "");
        } else {
            return Result.succ(400, "保存失败", "");
        }
    }


    @ApiOperation("上传检验图片")
    @PostMapping("/uploadImage")
    @LogDetail(operationType = OperationType.UPDATE, module = "上传检验图片")
    public Result uploadSignatureFile(@RequestParam(value = "file") MultipartFile file, HttpServletRequest request) throws IOException {
        if (file == null) {
            return Result.fail("文件不能为空");
        }
        // 文件限制10M
        long size = file.getSize();
        if (size > MAX_POST_SIZE) {
            return Result.fail("文件最大为 10M！");
        }
        String folder = picturePath + PropertySlash.getSlash();
        if (!FileUtil.exist(folder)) {
            FileUtil.mkdir(folder);
        }
        String fileName = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddhhmmssSSS")) + file.getOriginalFilename();
        String path = folder + fileName;
        File files = new File(path);
        if (FileUtil.exist(files)) {
            return Result.fail("文件已存在");
        }
        File file1 = FileUtil.writeBytes(file.getBytes(), path);
        if (file1.length() < 0) {
            return Result.fail("文件上传失败");
        }
        return Result.succ("/image/morphology/" + fileName);
    }
}
